﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using iBook2.Models;

namespace iBook2.Controllers
{
    public class AccountTypeController : ApiController
    {
        private iBookEntities db = new iBookEntities();

        // GET api/AccountType
        public IEnumerable<LoaiTaiKhoan> GetLoaiTaiKhoans()
        {
            return db.LoaiTaiKhoans.AsEnumerable();
        }

        // GET api/AccountType/5
        public LoaiTaiKhoan GetLoaiTaiKhoan(string id)
        {
            LoaiTaiKhoan loaitaikhoan = db.LoaiTaiKhoans.Find(id);
            if (loaitaikhoan == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return loaitaikhoan;
        }

        // PUT api/AccountType/5
        public HttpResponseMessage PutLoaiTaiKhoan(string id, LoaiTaiKhoan loaitaikhoan)
        {
            if (ModelState.IsValid && id == loaitaikhoan.MaLoaiTaiKhoan)
            {
                db.Entry(loaitaikhoan).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/AccountType
        public HttpResponseMessage PostLoaiTaiKhoan(LoaiTaiKhoan loaitaikhoan)
        {
            if (ModelState.IsValid)
            {
                db.LoaiTaiKhoans.Add(loaitaikhoan);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, loaitaikhoan);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = loaitaikhoan.MaLoaiTaiKhoan }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/AccountType/5
        public HttpResponseMessage DeleteLoaiTaiKhoan(string id)
        {
            LoaiTaiKhoan loaitaikhoan = db.LoaiTaiKhoans.Find(id);
            if (loaitaikhoan == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.LoaiTaiKhoans.Remove(loaitaikhoan);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, loaitaikhoan);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}